//
//  MQPopupView.m
//  MQ
//
//  Created by 曾杰 on 15/10/27.
//  Copyright © 2015年 徐永祥. All rights reserved.
//

#import "MQPopupView.h"
#import "LewPopupViewAnimationDrop.h"
#import "MQSettingMainViewController.h"
#import "MQFMDBManager.h"
#import <FMDB.h>
#import "MQAboutUsViewController.h"
#import <UIImageView+WebCache.h>
@implementation MQPopupView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //背景图
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.bounds];
        [imageView setImage:[UIImage imageNamed:@"shezhi"]];
        [self addSubview:imageView];
        UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [button1 setTitle:@"我的收藏" forState:(UIControlStateNormal)];
        [button1 addTarget:self action:@selector(cacheDetail:) forControlEvents:(UIControlEventTouchUpInside)];
        button1.frame = CGRectMake(0, 10, self.bounds.size.width, 30);
        [button1 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [button1 setTitleColor:[UIColor redColor] forState:(UIControlStateHighlighted)];
        self.backgroundColor = [UIColor redColor];
        [self addSubview:button1];
        //清除缓存按钮
        UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [button2 setTitle:@"清除缓存" forState:(UIControlStateNormal)];
        [button2 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [button2 setTitleColor:[UIColor redColor] forState:(UIControlStateHighlighted)];
        [button2 addTarget:self action:@selector(clearAllCache:) forControlEvents:(UIControlEventTouchUpInside)];
        button2.frame = CGRectMake(0, 50, self.bounds.size.width, 30);
        self.backgroundColor = [UIColor redColor];
        [self addSubview:button2];
        //夜间模式开关
        UIButton *nightButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        nightButton.frame = CGRectMake(0, 90, self.bounds.size.width, 30);
        [nightButton setTitle:@"日间模式" forState:(UIControlStateNormal)];
        [nightButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [nightButton setTitle:@"夜间模式" forState:(UIControlStateSelected)];
        nightButton.selected = YES;
        [nightButton addTarget:self action:@selector(nightType:) forControlEvents:(UIControlEventTouchUpInside)];
        [nightButton setTitleColor:[UIColor blackColor] forState:(UIControlStateSelected)];
        [self addSubview:nightButton];
        
        //关于我们按钮
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor redColor] forState:(UIControlStateHighlighted)];
        [button setTitle:@"关于我们" forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(aboutMe:) forControlEvents:(UIControlEventTouchUpInside)];
        button.frame = CGRectMake(0, 130, self.bounds.size.width, 30);
        self.backgroundColor = [UIColor redColor];
        [self addSubview:button];
        
    }
    return self;
}


+ (instancetype)defaultPopupView{
    return [[MQPopupView alloc]initWithFrame:CGRectMake(0, 0, 195, 210)];
}
//进入收藏页面
- (void)cacheDetail:(UIButton *)button
{
     [_parentVC lew_dismissPopupViewWithanimation:[LewPopupViewAnimationDrop new]];
    MQSettingMainViewController *settingVC = [[MQSettingMainViewController alloc]init];
    UINavigationController *naviSetting = [[UINavigationController alloc]initWithRootViewController:settingVC];
    [_parentVC presentViewController:naviSetting animated:YES completion:nil];
}
//清楚缓存
-(void)clearAllCache:(UIButton *)button
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"清除缓存成功" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:nil];
    [alert addAction:alertAction];
    [_parentVC presentViewController:alert animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [_parentVC dismissViewControllerAnimated:YES completion:nil];
    });
//     NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//    [user removeObjectForKey:@"zanArr"];
    //清除数据库里的数据
    [[MQFMDBManager shareDataBase] executeUpdate:@"DELETE FROM PICTURE"];
    NSString *dbpath = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingString:@"video.db"];
    
    _db = [FMDatabase databaseWithPath:dbpath];
    NSLog(@"%@",dbpath);
    if([_db open])
    {
        NSString *sql = [NSString stringWithFormat:@"create table  usrTable (dataName text primary key, jsonData blob)"];
        [_db executeUpdate:sql];
    }
    [_db executeUpdate:@"delete from usrTable"];
    [[SDImageCache sharedImageCache]cleanDisk];
    [[SDImageCache sharedImageCache]clearMemory];
    
}
//夜间模式
-(void)nightType:(UIButton *)button
{
    if (button.selected == YES)
    {
        [[UIScreen mainScreen] setBrightness:0.1];
    }
    else
    {
        NSUserDefaults *defa = [NSUserDefaults standardUserDefaults];
        CGFloat light = [defa floatForKey:@"brightness" ];
        [[UIScreen mainScreen] setBrightness:light];
    }
    button.selected = !button.selected;
    
}
//关于我们
-(void)aboutMe:(UIButton *)button
{
    MQAboutUsViewController *aboutUsVC = [[MQAboutUsViewController alloc] init];
    [_parentVC.navigationController presentViewController:aboutUsVC animated:YES completion:nil];
}


@end
